import pytest
from config.config import read_config
from utils.logger_util import Logger


@pytest.fixture(scope="function", autouse=True)
def setup():
    print("Setup")
    yield
    print("Teardown")


@pytest.fixture(scope="session", autouse=True)
def logger():
    """获取日志对象"""
    logger = Logger().get_logger()
    return logger


# 实现执行动态参数切换测试环境
def pytest_addoption(parser):
    """添加命令行参数 --env 来指定测试环境"""
    parser.addoption("--env", action="store", default="dev", help="Test environment")


@pytest.fixture(scope="session")
def config(request):
    """根据命令行参数 --env 读取不同的环境配置"""
    # 获取 pytest 运行时传递的 --env 参数的值,如pytest --env=prod，environment 的值就是 "prod"
    environment = request.config.getoption("--env")
    return read_config(environment)


@pytest.fixture(scope="session")
def wechat_base_url(config):
    """获取当前测试环境的 wechat_base_url"""
    return config.get("wechat_wechat_base_url")


@pytest.fixture(scope="session")
def httpbin_base_url(config):
    """获取当前测试环境的 httpbin_base_url"""
    return config.get("httpbin_wechat_base_url")
